package cn.wolfcode.service.impl;

import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.query.PageResult;
import cn.wolfcode.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public PageInfo<Department> query(QueryObject qo) {
        //不需要手动执行count语句,由分页插件来执行
      /*  //查询总条数
        int totalCount = departmentMapper.queryForCount(qo);
        //判断总条数是否为0
        if (totalCount==0) {
            return new PageResult(qo.getCurrentPage(),qo.getPageSize());
        }*/

        //告诉分页插件,当前页以及每页显示的条数就可以,分页插件会自动算出startIndex,放入limit中
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());//只对下一句sql进行自动分页
        //不需要自己写limit,由分页插件来执行
        List<Department> list = departmentMapper.queryForList(qo);
        //return new PageResult(qo.getCurrentPage(), qo.getPageSize(), totalCount, list);
        //返回的时候不用自己写pageResult,用分页插件提供好的PageInfo进行封装










        return new PageInfo<>(list);
    }

    @Override
    public void save(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);

    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }
}
